using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Zhao.Common.Helper;
using Zhao.IServer.Settlement;
using Zhao.Models.DTO.Settlement;

namespace Zhao.Arena.MiniProgram.Api.Controllers
{
    /// <summary>
    /// 结算控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize(Roles = "Admin")]
    public class SettlementController : ControllerBase
    {
        private readonly ISettlementService _settlementService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="settlementService">结算服务</param>
        public SettlementController(ISettlementService settlementService)
        {
            _settlementService = settlementService;
        }

        /// <summary>
        /// 手动触发结算
        /// </summary>
        /// <param name="date">指定结算日期，默认为当前日期</param>
        /// <returns>结算结果</returns>
        [HttpPost("execute")]
        public async Task<ApiResult<SettlementResultDto>> ExecuteSettlement(DateTime? date = null)
        {
            var result = new ApiResult<SettlementResultDto>();
            
            try
            {
                var settlementDate = date ?? DateTime.Today.AddDays(-1); // 默认结算昨天的记录
                var settlementResult = await _settlementService.ExecuteSettlementAsync(settlementDate);
                
                result.Success = settlementResult.FailedCount == 0;
                result.StatusCode = 200;
                result.Message = result.Success
                    ? $"结算成功，共处理{settlementResult.SuccessfulCount}条记录，总金额{settlementResult.TotalAmount}"
                    : $"结算部分成功，成功{settlementResult.SuccessfulCount}条，失败{settlementResult.FailedCount}条";
                result.Data = settlementResult;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.StatusCode = 500;
                result.Message = $"结算失败：{ex.Message}";
            }
            
            return result;
        }

        /// <summary>
        /// 查询指定日期的结算结果
        /// </summary>
        /// <param name="date">结算日期</param>
        /// <returns>结算记录统计</returns>
        [HttpGet("result/{date}")]
        public async Task<ApiResult> GetSettlementResult(DateTime date)
        {
            var result = new ApiResult<object>();
            
            try
            {
                // 查询该日期的结算记录
                // 此处可以根据需要扩展结算结果查询的实现
                
                result.Success = true;
                result.StatusCode = 200;
                result.Message = $"查询{date:yyyy-MM-dd}的结算结果成功";
                result.Data = new { Date = date, Status = "已完成" };
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.StatusCode = 500;
                result.Message = $"查询结算结果失败：{ex.Message}";
            }
            
            return result;
        }
    }
} 